<template>
  <div>
    <van-nav-bar title="商品详情" left-arrow @click-left="onClickLeft" />
    <van-swipe>
      <van-swipe-item v-for="(item, index) in message.imgArr" :key="index">
        <img :src="item" alt="" />
      </van-swipe-item>
      <template #indicator="{ active }">
        <div class="custom-indicator">{{ active + 1 }}/{{ num }}</div>
      </template>
    </van-swipe>
    <div class="describe">
      <span>￥{{ message.currentPrice }}.00</span>
      <p>普通价 ￥{{ message.originalPrice }}</p>
      <i>{{ message.productName }}</i>
      <div>{{ message.describe }}</div>
    </div>
    <van-collapse v-model="activeNames">
      <van-collapse-item title="选择 可选内容" name="1">内容</van-collapse-item>
      <van-collapse-item title="商品评论" name="2">内容</van-collapse-item>
    </van-collapse>
    <van-action-bar>
      <van-action-bar-icon icon="chat-o" text="客服" @click="onClickIcon" />
      <van-action-bar-icon icon="cart-o" text="购物车" @click="onClickIcon" />
      <van-action-bar-button
        type="warning"
        text="加入购物车"
        @click="onClickButton"
      />
      <van-action-bar-button
        type="danger"
        text="立即购买"
        @click="onClickButton"
      />
    </van-action-bar>
  </div>
</template>
<script>
import {
  ActionBar as VanActionBar,
  ActionBarIcon as VanActionBarIcon,
  ActionBarButton as VanActionBarButton,
  Toast,
  Swipe as VanSwipe,
  SwipeItem as VanSwipeItem,
  NavBar as VanNavBar,
  Collapse as VanCollapse,
  CollapseItem as VanCollapseItem,
} from "vant";
import { onMounted, ref } from "vue";
import { detailApi } from "../../utils/api";
export default {
  props: ["id"],
  setup(props) {
    const onClickIcon = () => Toast("点击图标");
    const onClickButton = () => Toast("点击按钮");
    const onClickLeft = () => Toast("-1");
    const id = ref(props.id);
    //存放数据的容器
    const message = ref("");
    const num = ref("1");
    const activeNames = ref([]);

    onMounted(() => {
      detailApi({productId:id.value}).then((res) => {
        console.log(res.result);
        message.value = res.result;
        num.value = res.result.imgArr.length;
      });
    });

    return {
      onClickIcon,
      onClickButton,
      onClickLeft,
      onMounted,
      message,
      num,
      activeNames,
    };
  },
  components: {
    VanActionBar,
    VanActionBarIcon,
    VanActionBarButton,
    VanSwipe,
    VanSwipeItem,
    VanNavBar,
    VanCollapse,
    VanCollapseItem,
  },
};
</script>

<style lang="less" scoped>
.custom-indicator {
  position: absolute;
  right: 5px;
  bottom: 5px;
  padding: 2px 5px;
  font-size: 12px;
  background: rgba(0, 0, 0, 0.1);
}
.van-swipe-item {
  background: #fafafa;
  text-align: center;
  img {
    margin: 0 auto;
  }
}
.custom-indicator {
  border-radius: 20px;
}
.describe {
  padding: 15px;
  background: #fafafa;
  span {
    font-size: 25px;
    color: red;
  }
  p {
    font-size: 15px;
    color: #888;
  }
  i {
    font-size: 16px;
    line-height: 16px;
    font-weight: 700;
  }
  div {
    font-size: 14px;
    color: #777;
  }
}
</style>
